package com.example.elm_server_api.controller;

import com.example.elm_server_api.domain.Food;
import com.example.elm_server_api.domain.Shop;
import com.example.elm_server_api.domain.ShopInfo;
import com.example.elm_server_api.service.IShopService;
import lombok.experimental.Delegate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/meituan")
@Slf4j
@CrossOrigin  //跨域
public class ShopController {

    @Autowired
    IShopService shopService;




    //商家的登录  定为一个号码一个店铺
    @GetMapping("/login")
    public Shop login(@RequestBody Shop shop, HttpServletRequest request,HttpSession session){
        log.info("{}",shop);

        Shop shop1 = this.shopService.selectByLog(shop);

        if (shop1 != null) {
            session.setAttribute("shop1",shop1);
            return shop1;
        }else {
            return null;
        }
//        return this.shopService.selectByLog(shop);
    }

    //商家的注册
    @PostMapping("/insertShop")
    public Integer insert( @RequestBody Shop shop){
        int insert = shopService.insert(shop);
        return insert;
    }

    //查询所有商家详细信息
    // @PreAuthorize("hasRole('ROLE_USER') and hasRole('ROLE_MANAGER')") //表达式表示同时满足
    // @Secured({"ROLE_MANAGER","ROLE_ADMIN","ROLE_SUPER_ADMIN"}) //不能用SpEL
    @GetMapping("/selectShopInforAll")
    @PreAuthorize("hasAnyRole('ROLE_SHOPER','ROLE_ADMIN')") // 任何一个满足
    public List<ShopInfo> selectShopInforAll(){
        return shopService.selectAll();
    }


    //单表商家的基本信息 ID 查询，避免手机号更改问题
    @GetMapping("/selectShopInforByShopID/{id}")
    @PreAuthorize("hasAnyRole('ROLE_SHOPER','ROLE_ADMIN')") // 任何一个满足
    public ShopInfo selectByShopID(@PathVariable("id") Long id){
        ShopInfo shopInfo = this.shopService.selectByShopID(id);
        log.info("{}",shopInfo);
        return shopInfo;
    }


    //商家密码、手机号修改 平台确认注册的更新
    @PutMapping("/updateByPrimaryKeySelective")
    @PreAuthorize("hasAnyRole('ROLE_SHOPER','ROLE_ADMIN')") // 任何一个满足
    public int updateByShopIdSelective(@RequestBody Shop record) {
        return shopService.updateByShopIdSelective(record);
    }


    //商家等几本信息的修改
    @PutMapping("/updateByMobileSelective")
    @PreAuthorize("hasAnyRole('ROLE_SHOPER','ROLE_ADMIN')") // 任何一个满足
    public int updateShopInfoSelective( @RequestBody ShopInfo record) {

        //从 session 中取出数据进行比较，如果修改了手机号和密码就直接返回登录页面，通过判断是否一样进行。前后台分离，已放弃
        /*Shop shop1 = (Shop)session.getAttribute("shop1");
        String mobile = shop1.getMobile();
        String password = shop1.getPassword();*/

        return shopService.updateShopInfoSelective(record);
    }


    //按 shopId 进行商店的删减  平台进行操作
    @DeleteMapping("/deleteShop/{id}")
    @PreAuthorize("hasAnyRole('ROLE_SHOPER','ROLE_ADMIN')") // 任何一个满足
    public int deleteShopByI (@PathVariable("id") Long id){
        int i = shopService.deleteByPrimaryKey(id);
        return i;
    }

    @GetMapping("/selectByName") //多表   商铺和食物的模糊查询
    @PreAuthorize("hasAnyRole('ROLE_USER','ROLE_SHOPER','ROLE_ADMIN')") // 任何一个满足
    public List<ShopInfo> selectByName( ShopInfo record) {

        List<ShopInfo> shopInfos = shopService.selectByName(record);

        /*for (ShopInfo shopInfo : shopInfos) {
            System.out.println(shopInfo);
            for (Food food : shopInfo.getFoods()) {
                if (food.getId() != null) {
                    System.out.println("         此用户下的订单有：" + food);
                }
            }
        }*/
        return shopInfos;
    }

}
